﻿Imports System.Xml

Public Class frmDelete
    Dim dokumen As New XmlDocument()
    Dim xfile As String = Application.StartupPath & "\Reminderxml.xml"
    Private Sub delete_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not My.Computer.FileSystem.FileExists(xfile) Then
            MessageBox.Show("No Reminder yet!", "Error")
            Close()
        Else
            loadTree()

        End If
        Me.CenterToScreen()
    End Sub

    Sub loadTree()
        With dokumen
            .Load(xfile)
            If .DocumentElement.ChildNodes.Count >= 1 Then
                Dim xRoot As New TreeNode(.DocumentElement.Name)
                With TreeView1
                    .Nodes.Clear()
                    .Nodes.Add(xRoot)
                    For Each i As XmlNode In dokumen.DocumentElement.ChildNodes
                        Dim xSchedule As New TreeNode("Schedule : " & i.Attributes(0).Value)
                        xRoot.Nodes.Add(xSchedule)
                        For Each j As XmlNode In i.ChildNodes
                            Dim xChild As New TreeNode(j.Name & " : " & j.InnerText)
                            xSchedule.Nodes.Add(xChild)
                        Next
                    Next
                    .ExpandAll()
                End With
            Else
                MessageBox.Show("No Reminder to delete!", "Error")
                Close()
            End If
        End With
    End Sub

  

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Hide()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Not IsNothing(TreeView1.SelectedNode) Then
            Dim xTeks As String = "Schedule : "
            If TreeView1.SelectedNode.Text.Contains(xTeks) Then
                Dim xIndex1 As String() = Split(TreeView1.SelectedNode.Text, xTeks)
                Dim xindex As Integer = xIndex1(UBound(xIndex1))
                Dim xSchedule As XmlElement = dokumen.SelectSingleNode("//Schedule[@id='" & xindex & "']")
                dokumen.DocumentElement.RemoveChild(xSchedule)
                dokumen.Save(xfile)
                loadTree()
            Else
                MessageBox.Show("only allowed to delete schedule!", "Error")
            End If
        Else
            MessageBox.Show("Please select schedule to delete!", "Error")
        End If

        frmMain.Form1_Load(sender, e)
    End Sub
End Class